package com.jamal2362.apktool.util.search;

import android.content.Context;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import androidx.appcompat.app.AlertDialog;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import com.jamal2362.apktool.R;
import com.jamal2362.apktool.fragment.files.FilesAdapter;
import com.jamal2362.apktool.lexer.LexerUtil;
import com.jamal2362.apktool.task.IconTask;
import com.jamal2362.apktool.util.FileUtils;
import com.jamal2362.apktool.util.IconFactory;
import com.jamal2362.apktool.util.IconTypes;
import com.jamal2362.apktool.util.MUtil;
import com.jamal2362.apktool.util.Settings;

public class SearchResult extends ListView {
	private LogAdapter adapter = null;
	private ArrayList<String> logarray = null;
	private Context mContext;
	private int textSize;
	private FilesAdapter refresh;
	private AlertDialog dialog;
	private boolean byName;
	private IconFactory iconFactory;

	public SearchResult(Context context, FilesAdapter refresh) {
		super(context);
		setDivider(null);
		setDividerHeight(0);
		this.mContext = context;
		this.refresh = refresh;
		this.byName = true;
		this.textSize = Settings.appFontSize;
		this.iconFactory = new IconFactory(mContext);
		this.iconFactory.round(Settings.roundIcons);
		this.logarray = new ArrayList<String>();
		this.adapter = new LogAdapter(context, R.id.details, logarray);
		this.setAdapter(adapter);
	}

	@Override
	public void setAdapter(ListAdapter adapter) {
		super.setAdapter(adapter);
	}

	public void setType(boolean byname) {
		this.byName = byname;
	}

	public void textSize(int size) {
		this.textSize = size;
	}

	public void setDialog(AlertDialog d) {
		this.dialog = d;
	}

	public void append(CharSequence s) {
		logarray.add(s.toString());
		setSelection(adapter.getCount() - 1);
	}

	public void append(List<String> list) {
		adapter = new LogAdapter(mContext, R.id.details, list);
		setAdapter(adapter);
	}

	public CharSequence getText() {
		return listToString(logarray);
	}

	public ArrayList<String> getTextArray() {
		return logarray;
	}

	public String listToString(ArrayList<String> list) {
		String listString = "";
		for (String s : list) {
			listString += s + "\n";
		}
		return listString;
	}

	private class LogAdapter extends ArrayAdapter<String> {
		private List<String> objects = null;

		public LogAdapter(Context context, int textviewid, List<String> objects) {
			super(context, textviewid, objects);
			this.objects = objects;
		}

		@Override
		public int getCount() {
			return ((null != objects) ? objects.size() : 0);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public String getItem(int position) {
			return ((null != objects) ? objects.get(position) : null);
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			View view = convertView;
			if (null == view) {
				LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				view = vi.inflate(R.layout.files_entry, null);
			}
			// view.setMinimumHeight(0);
			int dft = Settings.appFontSize - 4;
			int minh = MUtil.spAsPx(mContext, (Settings.appFontSize * 2 + dft));
			view.setMinimumHeight(minh);
			String data = objects.get(position);
			if (null != data && !TextUtils.isEmpty(data)) {
				final File file = new File(data);
				final TextView textview = view.findViewById(R.id.details);
				textview.setText(data);
				textview.setTextSize(TypedValue.COMPLEX_UNIT_SP, dft);
				final TextView name = view.findViewById(R.id.name);
				name.setText(file.getName());
				name.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
				final ImageView icon = view.findViewById(R.id.icon);
				icon.setTag(file.getAbsolutePath());
				if (file.isDirectory()) {
					icon.setImageBitmap(iconFactory.makeIcon((Settings.lightTheme ? IconTypes.colorFolderLight : IconTypes.colorFolderDark), R.drawable.ic_folder));
				} else {
					String n = data.toLowerCase();
					int[] types = IconTypes.getType(n);
					icon.setImageBitmap(iconFactory.makeIcon(types[1], types[0]));
					if (n.endsWith(".apk") || n.endsWith(".apks") || n.endsWith(".xapk") || types[0] == R.drawable.ic_image) {
						IconTask itask = new IconTask(mContext, icon);
						itask.execute(file);
					}
				}
				view.setOnClickListener(new OnClickListener()
					{
						@Override
						public void onClick(View p1) {
							if (file.isDirectory()) {
								refresh.refresh(file);
							} else {
								String type = FileUtils.parseFileName(file.getName())[1];
								if (FileUtils.isText(file) || LexerUtil.isText(type)) {
									refresh.getFrag().edit(file);
									dialog.cancel();
								} else {
									FileUtils.open(file, p1, refresh);
								}
							}
						}
					});
				view.setOnLongClickListener(new OnLongClickListener()
					{
						@Override
						public boolean onLongClick(View p1) {
							refresh.refresh(file.getParentFile());
							refresh.setHighlight(file.getAbsolutePath());
							refresh.setPosition(file.getAbsolutePath());
							dialog.cancel();
							return true;
						}
					});
			}
			return view;
		}
	}
}
